﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;
using PalProjects.SSIS.ControlFlow;

namespace PalProjects.SSIS.ControlFlow
{
    public partial class DropIndexEditor : Form
    {
        #region -- Private Variables
        private TaskHost taskHost;
        private DtsContainer parentContainer;
        private Connections connections;
        #endregion

        #region -- Constructor
        public DropIndexEditor(TaskHost taskHost, Connections connections)
        {
            this.taskHost = taskHost;
            this.connections = connections;
            this.parentContainer = taskHost.Parent;
            InitializeComponent();
        }
        #endregion

        #region -- Form Event Handlers
        private void RebuildIndexEditor_Load(object sender, EventArgs e)
        {
            cBxConnection.Items.Clear();
            foreach (ConnectionManager item in connections)
            {
                if (item.CreationName.Substring(0,3) == "ADO")
                {
                    cBxConnection.Items.Add(item.Name);   
                }
            }

            cBxScriptVariable.Items.Clear();
            foreach (Variable item in parentContainer.Variables)
            {
                if (!item.SystemVariable && item.DataType == TypeCode.String)
            	{
                    cBxScriptVariable.Items.Add(item.Name);
	            }
            }

            cBxConnection.Text = (string)taskHost.Properties["MDConnectionName"].GetValue(taskHost);
            cBxTable.Text = (string)taskHost.Properties["EntityName"].GetValue(taskHost);
            cBxScriptVariable.Text = (string)taskHost.Properties["ScriptVariable"].GetValue(taskHost);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            taskHost.Properties["MDConnectionName"].SetValue(taskHost, cBxConnection.Text);
            taskHost.Properties["EntityName"].SetValue(taskHost, cBxTable.Text);
            taskHost.Properties["MDConnectionString"].SetValue(taskHost, connections[cBxConnection.Text].ConnectionString);
            taskHost.Properties["ScriptVariable"].SetValue(taskHost, cBxScriptVariable.Text);

            DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cBxConnection_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dataTable;
            using (SqlConnection sqlConnection = new SqlConnection(connections[cBxConnection.Text].ConnectionString))
            {
                SqlCommand sqlCommand = new SqlCommand("SELECT [name] FROM sys.tables", sqlConnection);
                sqlCommand.CommandTimeout = 0;
                dataTable = new DataTable();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand);
                dataAdapter.Fill(dataTable);

                cBxTable.Items.Clear();
                foreach (DataRow item in dataTable.Rows)
                {
                    cBxTable.Items.Add(item["name"].ToString());
                }
            }
        }
    }
}
